<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="城市：">
          <el-select v-model="formInline.city" @change="getHospital" filterable clearable>
            <el-option v-for="(item,index) in cityList" :key="index" :label="item.name" :value="item.code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医院：">
          <el-select v-model="formInline.hospital" filterable clearable>
            <el-option v-for="(item,index) in hospitalList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人：">
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
        <el-form-item label="预约时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建预约时间：">
          <el-date-picker v-model="formInline.createTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户报名时间：">
          <el-date-picker v-model="formInline.applyTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收款时间：">
          <el-date-picker v-model="formInline.followTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="onSearch">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <!-- <el-button type="primary" size="small" :loading="eloading" @click="downloadExcel">导出筛选结果</el-button> -->
        <auth-button uid="1070101" type="primary" size="small" :loading="eloading" @click.native="downloadExcel">导出筛选结果</auth-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" show-summary :summary-method="getSummaries" :data="tableData">
        <el-table-column prop="channelName" label="医院信息" width="240">
          <template slot-scope="{row}">
            <tab-hospital :user="showFormatter(row)" :show="false"></tab-hospital>
          </template>
        </el-table-column>
        <el-table-column prop="principal" label="负责人" show-overflow-tooltip />
        <el-table-column prop="newAppointmentCnt" label="总预约数" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.newAppointmentCnt||0}}</template>
        </el-table-column>
        <el-table-column prop="appointmentCnt" label="到期预约数" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.appointmentCnt||0}}</template>
        </el-table-column>
        <el-table-column prop="arrivedCnt" label="到店人数" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.arrivedCnt||0}}</template>
        </el-table-column>
        <el-table-column prop="arrivedDealCnt" label="成交人数" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.arrivedDealCnt||0}}</template>
        </el-table-column>
        <el-table-column prop="dealAmount" label="成交金额" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.dealAmount?Number(row.dealAmount.toFixed(0)):row.dealAmount}}</template>
        </el-table-column>
        <el-table-column prop="collectMoney" label="收款金额" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.collectMoney?Number(row.collectMoney.toFixed(0)):row.collectMoney}}</template>
        </el-table-column>
        <el-table-column prop="appointmentArrivedRate" label="预约到店率" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.appointmentArrivedRate?row.appointmentArrivedRate.toFixed(0):0}}%</template>
        </el-table-column>
        <el-table-column prop="arrivedDealRate" label="到店成交率" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.arrivedDealRate?row.arrivedDealRate.toFixed(0):0}}%</template>
        </el-table-column>
        <el-table-column prop="averageCustomerUnitPrice" label="平均客单价" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.averageCustomerUnitPrice?row.averageCustomerUnitPrice.toFixed(0):0}}</template>
        </el-table-column>
        <el-table-column prop="mainDemandsCnt" label="矫正/种植成交人数" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.mainDemandsCnt||0}}</template>
        </el-table-column>
        <el-table-column prop="mainDemandsArrivedRate" label="矫正/种植到店成交率" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.mainDemandsArrivedRate?row.mainDemandsArrivedRate.toFixed(0):0}}%</template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

  </div>
</template>

<script>
import { GETCITYOPTIONS } from "@/http/system";
import { GETHOSPITALSEARCHSTAT, HOSPITALEXPORTSTAT } from "@/http/info";
import { GETHOSPITALOPTIONS } from "@/http/hospital";
import { showToast, showModal, deepClone } from "@/common/util";
import { exportExcel } from "@/common/common";
import GlsxDialog from "@/components/Dialog/index";
import moment from "moment";
import TabHospital from "@/components/hospital";
import AuthButton from "@/components/auth-button";
export default {
  name: "BookPage",
  props: {
    status: {
      type: [String, Number],
      default: "",
    },
  },
  components: {
    GlsxDialog,
    TabHospital,
    AuthButton,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      orderTime: [],
      orderTime2: [],
      showDialog: false,
      loading: false,
      eloading: false,
      cityList: [],
      hospitalList: [],
      summary: {},
    };
  },
  created() {
    let n = moment().format("YYYY-MM-DD");
    let min = moment(new Date()).subtract(3, "months").format("YYYY-MM-DD");
    this.$set(this.formInline, "orderTime", [min, n]);
  },
  mounted() {
    this.getList();
    this.getCity();
    this.getHospital();
  },
  methods: {
    /** 重置 */
    rest() {
      this.formInline = {};
      this.orderTime = [];
      this.getList();
      this.getHospital();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let oParams = deepClone(this.formInline);

      if (oParams.orderTime && oParams.orderTime.length > 0) {
        oParams.orderStartTime = oParams.orderTime[0];
        oParams.orderEndTime = oParams.orderTime[1];
      } else {
        delete oParams.orderStartTime;
        delete oParams.orderEndTime;
      }

      if (oParams.createTime && oParams.createTime.length > 0) {
        oParams.orderCreateStartTime = oParams.createTime[0];
        oParams.orderCreateEndTime = oParams.createTime[1];
      } else {
        delete oParams.orderCreateStartTime;
        delete oParams.orderCreateEndTime;
      }

      if (oParams.applyTime && oParams.applyTime.length > 0) {
        oParams.enrollStartTime = oParams.applyTime[0];
        oParams.enrollEndTime = oParams.applyTime[1];
      } else {
        delete oParams.enrollStartTime;
        delete oParams.enrollEndTime;
      }

      if (oParams.followTime && oParams.followTime.length > 0) {
        oParams.collectStartTime = oParams.followTime[0];
        oParams.collectEndTime = oParams.followTime[1];
      } else {
        delete oParams.collectStartTime;
        delete oParams.collectEndTime;
      }

      delete oParams.orderTime;
      delete oParams.createTime;
      delete oParams.applyTime;
      delete oParams.followTime;

      oParams.pageNumber = this.pageNumber;
      oParams.pageSize = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      GETHOSPITALSEARCHSTAT(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
        _this.summary = res.summary || {};
      });
    },

    // 格式化医院数据
    showFormatter(row) {
      if (!row) {
        return {};
      }
      row.hospitalName = row.name;
      row.hospitalAddress = row.address;
      return row;
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有医院统计列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile();
          },
        });
        return false;
      }
      showToast({
        message: "请选择筛选条件后再导出",
      });
    },

    // 下载excel
    dowloadExcelFile() {
      let _this = this;
      let oParams = this.getParams();
      let options = {};
      options.method = "GET";
      options.data = oParams;
      options.url = HOSPITALEXPORTSTAT;
      options.fileName = "医院统计列表.xlsx";
      _this.eloading = true;
      exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },

    // 获取所有城市
    getCity() {
      GETCITYOPTIONS().then((res) => {
        this.cityList = res.data || [];
      });
    },

    // 获取所有医院
    getHospital(code) {
      let oParams = {};
      oParams.city = code;
      if (this.formInline.hospital) {
        delete this.formInline.hospital;
      }
      GETHOSPITALOPTIONS(oParams).then((res) => {
        this.hospitalList = res.data || [];
      });
    },

    // 自定义展示合计
    getSummaries(param) {
      const { columns, data } = param;
      let summary = this.summary;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }

        if(column.property == 'appointmentArrivedRate'){
          sums[index] = (summary[column.property]?(summary[column.property]*100).toFixed(0):0) + '%';
          return
        }

        if(column.property == 'averageCustomerUnitPrice'){
          sums[index] = summary[column.property]?summary[column.property].toFixed(0):0;
          return
        }

        if(column.property == 'mainDemandsArrivedRate'){
          sums[index] = (summary[column.property]?(summary[column.property]*100).toFixed(0):0) + '%';
          return
        }
        sums[index] = summary[column.property];
      });
      return sums;
    },

    // 搜索
    onSearch(){
      this.pageNumber = 1;
      this.getList()
    }
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/style/customer/index.scss";
</style>
